package validators;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import core.IEntityValidator;


public class LeagueValidator implements IEntityValidator {
	
	private String message = "";

	public boolean validate(String[] data) {
		// The massiv of data about new League: String[] = {ownerId, name, discipline_id, date_of_inceprion, date_of_end}

		if(data.length!=5) return false;
		// verification of the absence of empty fields in data
		for(int i=0; i<data.length;i++){
			if (!valLength(data[i])) {
				message = "Fill in all required fields!";
				return false;
			}
		}
		
		// pattern for name
		Pattern p_letters = Pattern.compile("[a-zA-Zа-яА-Я\\d\\u002E\\u005F\\p{Blank}\\-]*");
		// pattern for date
		Pattern p_date = Pattern
				.compile("([0-9]{1,2})+\\.+([0-9]{1,2})+\\.+([0-9]{4})");
		// pattern for id
		Pattern p_id = Pattern.compile("[0-9]*");

		Matcher m_ownerId = p_id.matcher(data[0]);
		if(!m_ownerId.matches()) {
			message = "Identificator of owner should be number!";
			return false;
		}
		
		Matcher m_name = p_letters.matcher(data[1]);
		if(!m_name.matches()) {
			message = "Field 'Name' has an incorrect format!";
			return false;
		}
		
		Matcher m_disciplineId = p_id.matcher(data[2]);
		if(!m_disciplineId.matches()) {
			message = "Identificator of discipline should be number!";
			return false;
		}
		
		Matcher m_dateOfInception = p_date.matcher(data[3]);
		if(!m_dateOfInception.matches()) {
			message = "Field 'Date of inception' has an incorrect format";
			return false;
		}
		
		Matcher m_dateOfEnd = p_date.matcher(data[4]);
		if(!m_dateOfEnd.matches()) {
			message = "Field 'Date of end' has an incorrect format";
			return false;
		}
		
		message = "The operation completed successfully!";
		return true;
	}
	
	public String getMessage(){
		return message;
	}

	private boolean valLength(String data){
		return data.length()>0;
	}
	
}
